package com.von.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.von.entity.DictionaryGroup;
import com.von.entity.DictionaryItem;
import com.von.enums.ApiErrorCode;
import com.von.enums.CommonEnum;
import com.von.excel.exportExcel.ExDictionaryItemExcel;
import com.von.excel.importExcel.ImDictionaryItemExcel;
import com.von.mapper.DictionaryItemMapper;
import com.von.param.DictionaryItemParam;
import com.von.result.R;
import com.von.service.IDictionaryGroupService;
import com.von.service.IDictionaryItemService;
import com.von.util.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 字典-项 服务实现类
 * </p>
 *
 * @author xiaosai
 * @since 2021-07-09
 */
@Service
public class DictionaryItemServiceImpl extends ServiceImpl<DictionaryItemMapper, DictionaryItem> implements IDictionaryItemService {

    @Autowired
    private IDictionaryGroupService iDictionaryGroupService;

    @Override
    public R<Page<DictionaryItem>> list(DictionaryItemParam dictionaryItemParam) {
        if(dictionaryItemParam!=null){
            return R.ok(lambdaQuery().eq(DictionaryItem::getGroupId,dictionaryItemParam.getGroupId()).orderByAsc(DictionaryItem::getId)
                    .page(new Page<>(dictionaryItemParam.getPageNum(),dictionaryItemParam.getPageSize())));
        }
        return R.failed(ApiErrorCode.FAILED);
    }

    @Override
    public R<String> add(DictionaryItem dictionaryItem) {
        if(dictionaryItem!=null){
            if(dictionaryItem.getGroupId()==null){
                return R.failed(CommonEnum.GROUPID_IS_NOT_EMPTY.getMsg());
            }
            if(StrUtil.isEmpty(dictionaryItem.getCode())){
                return R.failed(CommonEnum.CODE_IS_NOT_EMPTY.getMsg());
            }
            if(StrUtil.isEmpty(dictionaryItem.getValue())){
                return R.failed(CommonEnum.VALUE_IS_NOT_EMPTY.getMsg());
            }
            if(StrUtil.isNotEmpty(dictionaryItem.getCode())){
                DictionaryItem one = lambdaQuery().eq(DictionaryItem::getGroupId, dictionaryItem.getGroupId()).eq(DictionaryItem::getCode, dictionaryItem.getCode()).one();
                if(one==null){
                    save(dictionaryItem);
                    return R.ok(ApiErrorCode.OK);
                }else {
                    return R.failed(CommonEnum.DICTIONARYITEM_REPETITION.getMsg());
                }
            }
        }
        return R.failed(ApiErrorCode.FAILED);
    }

    @Override
    public R<String> update(DictionaryItemParam dictionaryItemParam) {
        if(dictionaryItemParam.getId()!=null){
            DictionaryItem dictionaryItem = Convert.convert(DictionaryItem.class, dictionaryItemParam);
            dictionaryItem.setUpdateTime(LocalDateTime.now());
            updateById(dictionaryItem);
            return R.ok(ApiErrorCode.OK);
        }
        return R.failed(ApiErrorCode.FAILED);
    }

    @Override
    public R<String> del(Integer id) {
        removeById(id);
        return R.ok(ApiErrorCode.OK);
    }

    @Override
    public void exportDictionaryItem(HttpServletResponse response, Integer groupId) {
        List<DictionaryItem> list = lambdaQuery().eq(DictionaryItem::getGroupId, groupId).list();
        if(CollUtil.isNotEmpty(list)){
            DictionaryGroup dictionaryGroup = iDictionaryGroupService.getById(groupId);
            List<ExDictionaryItemExcel> exDictionaryItemExcels = Convert.convert(new TypeReference<List<ExDictionaryItemExcel>>() {
            }, list);
            ExcelUtils.exportExcel2LocalDateTime(response, ExDictionaryItemExcel.class, exDictionaryItemExcels,
                    dictionaryGroup.getName()+".xlsx",dictionaryGroup.getName());
        }
    }

    @Override
    public void downloadDictionaryItem(HttpServletResponse response) {
        ExcelUtils.exportExcel2LocalDateTime(response, ImDictionaryItemExcel.class,new ArrayList<>(),
                "字典子项导入模板.xlsx","模板");
    }

    @Override
    public R<String> importDictionaryItem(MultipartFile file, Integer groupId) {
        List<ImDictionaryItemExcel> imDictionaryItemExcels = ExcelUtils.importExcel(file, ImDictionaryItemExcel.class);
        if(CollUtil.isNotEmpty(imDictionaryItemExcels)){
            List<DictionaryItem> dictionaryItems = Convert.convert(new TypeReference<List<DictionaryItem>>() {
            }, imDictionaryItemExcels);
            dictionaryItems.forEach(dictionaryItem -> dictionaryItem.setGroupId(groupId));
            saveBatch(dictionaryItems);
            return R.ok(ApiErrorCode.OK);
        }
        return R.failed(ApiErrorCode.FAILED);
    }
}
